package com.spring.sunflower.common;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.bean.AccountOffLocalBean;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.dialog.NoticeDialog;
import com.spring.sunflower.dialog.OnPermissionIntroClickListener;
import com.spring.sunflower.util.AppUtils;

import java.util.List;

/**
 * Created by Kevin on 2021/6/28<br/>
 * <p>
 * Describe:<br/>
 */
public class AccountOffActivity extends BaseActivity<AccountOffPresenter> implements CommonContract.AccountOffView {
    private TextView tvAccountOff, tvSecond;
    private CountDownTimer countDownTimer;
    private boolean isOnTick = true;
    private LinearLayout llOffAccount;
    private List<AccountOffLocalBean.DataBean> data;
    private StringBuilder stringBuilder;
    private TextView tvOne, tvTwo;

    @Override
    public void initView() {
        String userID = getField(Constant.KEY_USERID, "");
        String mobile = getField(Constant.KEY_MOBILE, "");
        tvTitle.setText("账号注销");
        tvSecond = findViewById(R.id.tvSeconds);
        llOffAccount = findViewById(R.id.llOffAccount);
        tvOne = findViewById(R.id.tvOne);
        tvTwo = findViewById(R.id.tvTwo);
        tvOne.setText(String.format(getText(R.string.account_off_tip_2).toString(), AppUtils.getAppName(this)));
        tvTwo.setText(String.format(getText(R.string.account_off_tip_4).toString(), AppUtils.getAppName(this)));
        llOffAccount.setBackground(getDrawable(R.drawable.ic_btn_bg_click_disable));
        llOffAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isOnTick) {
//                    new XPopup.Builder(AccountOffActivity.this)
//                            .isDestroyOnDismiss(true)
//                            .isRequestFocus(false)
//                            .asConfirm("请确认删除账号操作", "您的账号将不再会被任何人看到，并且所有信息将会被删除", "取消", "确定", new OnConfirmListener() {
//                                @Override
//                                public void onConfirm() {
//                                    saveField(Constant.KEY_TOKEN, "");
//                                    saveField(Constant.KEY_USERID, "");
//                                    Intent intent = new Intent(AccountOffActivity.this, LoginActivity.class);
//                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                    startActivity(intent);
//                                }
//                            }, null, false)
//                            .show();
                    NoticeDialog dialog = showNoticeDialog("账号注销结果", "您的账号将不再会被任何人看到，并且所有信息将会被删除", new OnPermissionIntroClickListener() {
                        @Override
                        public void onClickCancel() {

                        }

                        @Override
                        public void onClickOK() {
//                            String ao = getField(Constant.KEY_ACCOUNT_OFF, "");
//                            stringBuilder = new StringBuilder();
//                            String time = String.valueOf(System.currentTimeMillis());
//                            if (!TextUtils.isEmpty(ao)) {
//                                String[] split = ao.split(",");
//                                List<String> list = Arrays.asList(split);
//                                if (!list.contains(userID)) {
//                                    stringBuilder.append(ao);
//                                    stringBuilder.append(",");
//                                    stringBuilder.append(userID);
//                                    stringBuilder.append("&");
//                                    stringBuilder.append(mobile);
//                                    stringBuilder.append("&");
//                                    stringBuilder.append(time);
////                                }
//                                } else {
//                                    stringBuilder.append(userID);
//                                    stringBuilder.append("&");
//                                    stringBuilder.append(mobile);
//                                    stringBuilder.append("&");
//                                    stringBuilder.append(time);
//                                }
//                            } else {
//                                stringBuilder.append(userID);
//                                stringBuilder.append("&");
//                                stringBuilder.append(mobile);
//                                stringBuilder.append("&");
//                                stringBuilder.append(time);
//                            }
//                            saveField(Constant.KEY_ACCOUNT_OFF, stringBuilder.toString());//存储userID
                            mPresenter.accountOff();
                        }
                    });
                    dialog.setHeadTitle("账号注销提示");

                }
            }
        });

        startCountDown();
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_account_off;
    }

    @Override
    public AccountOffPresenter setPresenter() {
        return new AccountOffPresenter(this);
    }

    public void startCountDown() {
        isOnTick = true;
        tvSecond.setVisibility(View.VISIBLE);
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvSecond.setText(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                isOnTick = false;
                tvSecond.setVisibility(View.GONE);
                llOffAccount.setClickable(true);
                llOffAccount.setBackground(getDrawable(R.drawable.ic_btn_bg_click_enable));
            }
        };
        countDownTimer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }

    @Override
    public void accountOffSuccess() {
        showToast("注销成功");
        backLogin();
    }
}
